export function isObj(val) {
	return Object.prototype.toString.call(val) === '[object Object]';
}

export function deepMergeObj(targetObj, newObj) {
	for (let i in newObj) {
		const val = newObj[i];
		if (isObj(val)) {
			const targetVal = targetObj[i];
			if (isObj(targetVal)) {
				deepMergeObj(targetVal, val);
			} else {
				targetObj[i] = val;
			}
		} else {
			targetObj[i] = val;
		}
	}
}

export function isNullOrUndefOrEmpty(v) {
	return v === "" || v === undefined || v === null;
}

export function getRange(start, end, {
	step = 1,
	formatter
}) {
	const result = [];
	for (let i = start; i <= end; i = i + step) {
		result.push(formatter ? formatter(i) : i);
	}
	return result;
}

export function getRandom(min, max) {
	let r = Math.random() * (max - min);
	let re = Math.round(r + min);
	re = Math.max(Math.min(re, max), min);
	return re;
}

export function paddingZero(str, len = 2) {
	str = str + "";
	let strLen = str.length;
	if (strLen >= len) return str;
	return new Array(len - strLen + 1).join('0') + str;
}

export function handleJSONFields(obj, keys, isObj) {
	keys.forEach((key) => {
		const value = obj[key];
		if (typeof value == 'string' && value) {
			try {
				obj[key] = JSON.parse(value);
			} catch (err) {
				console.log('handleJSONFields parse err', key, obj);
			}
		}
		if (!obj[key]) {
			obj[key] = isObj ? {} : [];
		}
	});
}

export function handleTempalteValue(value, subValue) {
	if (isNullOrUndefOrEmpty(value)) return "--";
	if (Array.isArray(value)) {
		return value.join(",");
	} else if (subValue) {
		return `${value}(${subValue})`;
	}
	return value;
}


export function copySimpleData(data) {
	if (isObj(data)) {
		return Object.assign({}, data)
	}
	if (Array.isArray(data)) {
		const result = [];
		data.forEach((el) => {
			result.push(Object.assign({}, el));
		});
		return result;
	}
	return data;
}

export function getValueWithPhone(value, phone) {
	if (isNullOrUndefOrEmpty(value)) return '--';
	if (!phone) {
		return value;
	}
	return value + '(' + phone + ')';
}

export function makePhone(phone) {
	phone && uni.makePhoneCall({
		phoneNumber: phone,
	});
}

/**
 * 将数值保留N位小数并返回，最后的0去掉
 * getFloatInteger(3.22222,2) => 3.22
 * getFloatInteger(3.2,2) => 3.2
 * getFloatInteger(3,2) => 3
 */
export function getFloatInteger(value, suf) {
	value = Number(value);
	if (!value && value !== 0) {
		return "--";
	}
	let result = value.toFixed(suf || 2);
	let arr = result.split(".");
	if (parseInt(arr[1]) === 0) {
		return Number(arr[0]);
	} else {
		return Number(`${arr[0]}.${arr[1].replace(/[0]+$/, "")}`);
	}
}

export function numberFormat(value) {
	value = +value;
	if (value === 0) return 0;
	return value ? new Intl.NumberFormat().format(value) : "--";
}

export function cloneData(obj) {
	var o;
	if (typeof obj == "object") {
		if (obj === null) {
			o = null;
		} else {
			if (obj instanceof Array) {
				o = [];
				for (var i = 0, len = obj.length; i < len; i++) {
					o.push(cloneData(obj[i]));
				}
			} else {
				o = {};
				for (var j in obj) {
					o[j] = cloneData(obj[j]);
				}
			}
		}
	} else {
		o = obj;
	}
	return o;
}

export function isEmptyObj(obj) {
	for (let i in obj) {
		return false;
	}
	return true;
}

export function intlNumberFormat(value, options) {
  value = Number(value);
  if (!value) {
    return value;
  }
  options = Object.assign(
    {
      maximumFractionDigits: 2,
    },
    options || {}
  );
  const formatter = new Intl.NumberFormat(undefined, options);
  return formatter.format(value);
}